package com.milk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.milk.context.BaseContext;
import com.milk.entity.Milktea;
import com.milk.entity.UserCollect;
import com.milk.entity.UserCoupon;
import com.milk.mapper.MilkteaMapper;
import com.milk.mapper.UserCollectMapper;
import com.milk.mapper.UserCouponMapper;
import com.milk.result.Result;
import com.milk.service.IUserCouponService;
import com.milk.service.UserCollectService;
import com.milk.vo.UserCollectVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class UserCollectServiceImpl extends ServiceImpl<UserCollectMapper, UserCollect> implements UserCollectService {
    @Autowired
    private UserCollectMapper userCollectMapper;
    @Autowired
    private MilkteaMapper milkteaMapper;
    /**
     * 添加收藏
     * @param userCollect
     */
    public void insertUserCollect(UserCollect userCollect) {
        userCollect.setUserId(BaseContext.getCurrentId());
        userCollectMapper.insert(userCollect);
    }

    /**
     * 查看收藏信息
     * @return
     */
    public List<UserCollectVO> selectUserCollect() {
        Long userId = BaseContext.getCurrentId();
        QueryWrapper<UserCollect> qw = new QueryWrapper<>();
        qw.eq("user_id",userId);
        List<UserCollect> userCollects = userCollectMapper.selectList(qw);
        List<UserCollectVO> list = new ArrayList<>();

        for (UserCollect userCollect : userCollects) {
            UserCollectVO userCollectVO = new UserCollectVO();
            Milktea milktea = milkteaMapper.selectById(userCollect.getMilkteaId());
            String cup = userCollect.getMilkteaFlavor().split(",")[0];
            userCollectVO.setMilkteaId(milktea.getId());
            userCollectVO.setMilkteaFlavor(userCollect.getMilkteaFlavor());
            userCollectVO.setName(milktea.getName());
            userCollectVO.setImage(milktea.getImage());
            if (Objects.equals(cup, "小杯")) {
                userCollectVO.setAmount(milktea.getPriceMin());
            } else if (Objects.equals(cup, "中杯")) {
                userCollectVO.setAmount(milktea.getPriceMedium());
            } else if (Objects.equals(cup, "大杯")) {
               userCollectVO.setAmount(milktea.getPriceMax());
            }
            list.add(userCollectVO);
        }
        return list;
    }

    /**
     * 判断用户是否收藏此口味
     * @param userCollect
     * @return
     */
    public Boolean isCollect(UserCollect userCollect) {
        QueryWrapper<UserCollect> qw = new QueryWrapper<>();
        qw.eq("milktea_flavor",userCollect.getMilkteaFlavor());
        qw.eq("user_id",BaseContext.getCurrentId());
        qw.eq("milktea_id",userCollect.getMilkteaId());

        List<UserCollect> userCollects = userCollectMapper.selectList(qw);

        if (userCollects == null || userCollects.isEmpty()){
            return false;
        }
        return true;
    }

}
